home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 May: Tool Chest / Developer CD Series May 1996 (Tool Chest) (Apple Computer) (1996).iso / Tool Chest / Testing & Debugging / Debuggers & dcmds / MacsBug 6.5.2 / dcmds / C Samples / Printf.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-07-26  |  3.2 KB  |  129 lines  |  [TEXT/MPS ]

  1. /*
  2.     File:        Printf.c
  3.  
  4.     Contains:    A dcmd that supports the C concept of printf.
  5.  
  6.     Written by:    JM3 = Jim Murphy
  7.                 sad = Scott Douglass
  8.  
  9.     Copyright:    © 1989, 1994-1995 by Apple Computer, Inc., All Rights Reserved.
  10.  
  11.     Change History (most recent first):
  12.  
  13.          <3>     3/26/95    DAL        Included <:Public:Strings.h> to avoid p2cstr warning (should fix
  14.                                     header file names). Used new dcmdFillVersion and dcmdFillString
  15.                                     macros.
  16.          <2>   10-Dec-94    JM3        Updated for new format 3 dcmd requirements.
  17.          <1>     2/16/89    sad        written
  18.  
  19. */
  20.  
  21. /*    This is mostly to show how dcmds can use the standard C library.
  22.  
  23.     The following MPW commands will build the dcmd and copy it to the
  24.     "Debugger Prefs" file in the System folder. The dcmd's name in
  25.     MacsBug will be the name of the file built by the Linker.
  26.     You must first copy dcmd.h, dcmdGlue.a.o and DRunTime.o from the
  27.     C Samples folder into this folder.
  28.  
  29.     C Put.c
  30.     C Printf.c
  31.     Link dcmdGlue.a.o Printf.c.o put.c.o -d DRuntime.o "{Libraries}"Interface.o ∂
  32.          "{CLibraries}StdCLib.o" "{CLibraries}CInterface.o" -sg Main=STDIO -o Printf
  33.     BuildDcmd Printf 1004
  34.     Echo 'include "Printf";'    |    Rez -a -o "{systemFolder}Debugger Prefs"
  35. */
  36.  
  37. #include <Types.h>
  38.  
  39. #include <stdio.h>
  40. #include <string.h>
  41. #include <:Public:Strings.h>
  42. #include <Memory.h>
  43.  
  44. #include "dcmd.h"
  45. #include "put.h"
  46.  
  47. pascal void CommandEntry(dcmdBlock* paramPtr)
  48. {
  49.  
  50.     static const char usageStr[] = "\p\"format\" arg...";
  51.  
  52.     switch (paramPtr->request)
  53.     {
  54.         case dcmdInit:
  55.             break;
  56.  
  57.         case dcmdHelp:
  58.             dcmdDrawLine("\pDisplays the arguments according to the format (no floating point).");
  59.             break;
  60.  
  61.         case dcmdGetInfo:
  62.             dcmdFillVersion(paramPtr, 0x03008000);    // version 3.0
  63.             dcmdFillString(paramPtr, usageStr, usageStr);
  64.             break;
  65.  
  66.         case dcmdDoIt:
  67.         {
  68.             Str255 formatstring;
  69.             unsigned char* formatp = formatstring;
  70.             Str255 formattedstring;
  71.  
  72.             dcmdSwapWorlds();            // not really necessary because we don’t access machine state
  73.  
  74.             (void)dcmdGetNextParameter(formatstring);
  75.  
  76.             p2cstr(formatstring);
  77.  
  78.             while (*formatp != '\0')
  79.             {
  80.                 char aformatspec[256];
  81.                 size_t speclength;
  82.                 long arg;
  83.                 Boolean ok;
  84.  
  85.                 // Find the next format spec (i.e. %d) and print out anything before it
  86.             
  87.                 char* nextp = strchr(formatp, '%');
  88.                 if (nextp == nil) nextp = formatp + strlen(formatp);
  89.  
  90.                 PutBytesTo(formatp, nextp - formatp, 0);
  91.                 formatp = nextp;
  92.  
  93.                 // Find the length of the format spec
  94.             
  95.                 speclength = strcspn(formatp + 1, "diouxXcspP%") + 2;    // we don’t do "feEgGn"
  96.                 memcpy(aformatspec, formatp, speclength);
  97.                 aformatspec[speclength] = '\0';
  98.                 (void)dcmdGetNextExpression(&arg, &ok);
  99.                 if (!ok) break;
  100.  
  101.                 sprintf(formattedstring, aformatspec, arg);
  102.                 PutCStr(formattedstring);
  103.                 formatp = formatp + speclength;
  104.             }
  105.             PutLine();
  106.  
  107.             dcmdSwapWorlds();
  108.         }
  109.             break;
  110.  
  111.         // Version 3 and newer dcmds must quietly ignore requests we don't recognize.
  112.     
  113.         default:
  114.             break;
  115.     }
  116.  
  117. } // CommandEntry
  118.  
  119. /*
  120.     the following are stubs to override the C library routines so that the dcmd isn’t
  121.     so big.
  122. */
  123.  
  124. size_t fwrite (const void *, size_t, size_t, FILE *) { return 0; }        // wont’t actually be called by sprintf
  125. _flsbuf() {}    // wont’t actually be called by sprintf
  126.  
  127. fcvt() {}        // used only for floating point %f, etc.
  128. ecvt() {}        // used only for floating point %e, etc.
  129.